package io.gitee.hefren.hhsharding.config;

import io.gitee.hefren.hhsharding.datasource.HhShardingDataSource;
import io.gitee.hefren.hhsharding.engine.HhShardingEngine;
import io.gitee.hefren.hhsharding.engine.impl.HhStandardShardingEngine;
import io.gitee.hefren.hhsharding.mybatis.HhShardingStatementHandlerInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @Date 2024/8/16
 * @Author lifei
 */
@Configuration
@EnableConfigurationProperties({HhShardingProperties.class})
public class HhShardingAutoConfiguration {

    @Bean
    public DataSource dataSource(HhShardingProperties hhShardingProperties) {
        return new HhShardingDataSource(hhShardingProperties);
    }

    @Bean
    public HhShardingEngine hhShardingEngine(HhShardingProperties hhShardingProperties) {
        return new HhStandardShardingEngine(hhShardingProperties);
    }

    @Bean
    public Interceptor statementHandlerInterceptor() {
        return new HhShardingStatementHandlerInterceptor();
    }
}
